//
//  HTLongGoodsCell.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTLongGoodsCell.h"
@interface HTLongGoodsCell()
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UILabel *destiLabel;
@property (weak, nonatomic) IBOutlet UILabel *detialLabel;

@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;
@property (weak, nonatomic) IBOutlet UIButton *thirdButton;

@end
@implementation HTLongGoodsCell

- (void)setDeliverGoodsModel:(HTDeliverGoodsModel *)deliverGoodsModel{
    _deliverGoodsModel = deliverGoodsModel;
    self.startLabel.text = deliverGoodsModel.fromadd;
    self.destiLabel.text = deliverGoodsModel.toadd;
    NSString *length = [HTUtils sortLengthDisplayStringWith:deliverGoodsModel.heiht];
    NSString *type = @"不限车型";
    if (!(deliverGoodsModel.type == nil || deliverGoodsModel.type.length <= 0)) {
        type = deliverGoodsModel.type;
    }
    NSString *dun = @"";
    if ([deliverGoodsModel.utit isEqualToString:@"1"]) {
        dun =[NSString stringWithFormat:@"%@吨",deliverGoodsModel.weight];
    }else{
        dun =[NSString stringWithFormat:@"%@方",deliverGoodsModel.weight];
    }
    NSString *cartype = @"";
    if (!(deliverGoodsModel.cartype == nil || deliverGoodsModel.cartype.length <= 0)) {
        cartype = deliverGoodsModel.cartype;
    }
    self.detialLabel.text = [NSString stringWithFormat:@"%@ %@ / %@ %@ / 运费%@元",length,type,dun,cartype,deliverGoodsModel.pricers];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (IBAction)didClickFirstButton:(id)sender {
    //编辑
    if (self.delegete && [self.delegete respondsToSelector:@selector(didClickEditGoods:)]) {
        [self.delegete didClickEditGoods:self.deliverGoodsModel];
    }
}
- (IBAction)didClickThirdButton:(id)sender {
    [UIAlertController alertStyleShowInController:self.controller title:@"温馨提示" message:@"你确定删除该货源消息吗？" alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
        [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //删除
            [self netloadFrequently];
        }]];
        [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    } completion:nil];
}
- (IBAction)didClickSecondButton:(id)sender {
    //发货
     [self netLoadDeliver];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
//删除常发货源
- (void)netloadFrequently{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestDeliverGoodsDeleteWithWithProductid:self.deliverGoodsModel.orderid success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"删除常发货源：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"删除常发货源失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    } failure:^(NSError * _Nonnull error) {
         [hud hideAnimated:YES];
    }];
}
//发布
- (void)netLoadDeliver{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestDeliverGoodsUsedWithProductid:self.deliverGoodsModel.orderid success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"货源状态更改：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"货源状态变更失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    } failure:^(NSError * _Nonnull error) {
         [hud hideAnimated:YES];
    }];
    
}

@end
